/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.panes;

import com.eng.scc.core.data.Address;
import com.eng.scc.core.data.City;
import com.eng.scc.core.data.HomeState;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.view.ApplicationContext;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultComboBoxModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Rafael
 */
public class AddressPane extends javax.swing.JPanel
{
    private Address address;

    /**
     * Creates new form AddressPane
     */
    public AddressPane()
    {
        initComponents();
        
        try
        {
            cbState.setModel( new DefaultComboBoxModel( FuelControllerManagerService.getInstance().getHomeStates().toArray() ) );
            updateCities();
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }

    public void setAddress( Address address )
    {
        try
        {
            this.address = address;
            
            tfCEP.setText( address.getCep() );
            tfNumber.setText( address.getNumber() );

            if ( address.getHomeState() > 0 )
            {
                cbState.setSelectedItem( FuelControllerManagerService.getInstance().getHomeState( address.getHomeState() ) );
                updateCities();
            }
            
            if ( address.getCityId() > 0 )
            {
                cbCity.setSelectedItem( FuelControllerManagerService.getInstance().getCity( address.getCityId() ) );
            }
            
            tfAddressName.setText( address.getName() );
        }
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    public Address getAddress()
    {   
        address.setCep( tfCEP.getText() );
        address.setNumber( tfNumber.getText() );
        address.setCityId( ((City)cbCity.getSelectedItem()).getId() );
        address.setHomeState( ((HomeState)cbState.getSelectedItem()).getId() );
        address.setName( tfAddressName.getText() );
        
        return address;
    }
    
    private void updateCities()
    {
        try
        {
            List<City> cs = FuelControllerManagerService.getInstance().getCities( (( HomeState) cbState.getSelectedItem()).getId() );
            
            if ( cs != null )
            {
                cbCity.setModel( new DefaultComboBoxModel( cs.toArray() ));
            }
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    public void validateForm( com.eng.scc.core.util.Error error )
    {
        if ( cbCity.getSelectedItem() == null )
        {
            error.add( "Informe a cidade!" );
        }
        
        if ( cbState.getSelectedItem() == null )
        {
            error.add( "Informe o Estado!" );
        }
        
        if ( tfAddressName.getText() == null || tfAddressName.getText().isEmpty() )
        {
            error.add( "Informe o Endereço!" );
        }
        
        if ( tfCEP.getText().replace( "-", "" ).trim().isEmpty() )
        {
            error.add( "Informe o Cep!" );
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        cbCity = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cbState = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tfAddressName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfNumber = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfCEP = new com.eng.scc.core.parts.FormattedTextField();
        try
        {
            tfCEP.setFormatterFactory( new DefaultFormatterFactory( new MaskFormatter( "#####-###" ) ) );
        }
        catch ( ParseException ex )
        {
            Logger.getLogger( AddressPane.class.getName() ).log( Level.SEVERE, null, ex );
        }
        
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Endereço", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(0, 51, 255))); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Cidade:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel1, gridBagConstraints);

        cbCity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(cbCity, gridBagConstraints);

        jLabel2.setText("Estado:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel2, gridBagConstraints);

        cbState.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbState.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cbStateActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(cbState, gridBagConstraints);

        jLabel3.setText("Endereço:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel3, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfAddressName, gridBagConstraints);

        jLabel4.setText("Nº:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel4, gridBagConstraints);

        tfNumber.setMinimumSize(new java.awt.Dimension(40, 20));
        tfNumber.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfNumber, gridBagConstraints);

        jLabel5.setText("CEP:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jLabel5, gridBagConstraints);

        tfCEP.setMinimumSize(new java.awt.Dimension(120, 20));
        tfCEP.setPreferredSize(new java.awt.Dimension(120, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(tfCEP, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void cbStateActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cbStateActionPerformed
    {//GEN-HEADEREND:event_cbStateActionPerformed
        updateCities();
    }//GEN-LAST:event_cbStateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbCity;
    private javax.swing.JComboBox cbState;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField tfAddressName;
    private com.eng.scc.core.parts.FormattedTextField tfCEP;
    private javax.swing.JTextField tfNumber;
    // End of variables declaration//GEN-END:variables
}
